package com.ihcil.forecast.demo.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 * Create by lotus on 2021/8/13 20:10
 * <p>
 * md5加密
 */

@Component
public class MD5Utils {

    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    private static final String salt = "1a2b3c4d";

    // 前端页面加密
    public static String inputpass2pass(String pass) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + pass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    // 2次加密
    public static String pass2DBpass(String pass, String salt) {
        String str = "" + salt.charAt(0) + salt.charAt(2) + pass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    // 存入数据库的密码
    public static String inputpass2DBpass(String pass, String salt) {
        String frompass = inputpass2pass(pass);
        String dbpass = pass2DBpass(frompass, salt);
        return dbpass;
    }

    public static void main(String[] args) {
        System.out.println(inputpass2pass("123456"));
        System.out.println(pass2DBpass("d3b1294a61a07da9b49b6e22b2cbd7f9", "1a2b3c4d"));
        System.out.println(inputpass2DBpass("123456", "1a2b3c4d"));
    }

}
